﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace Common
{
    public class CommonHandler
    {

        public static object GetException(string strjson, string key, object defaultvalue)
        {
            try
            {
                JObject obj = JObject.Parse(strjson);

                return obj[key] ;
            }
            catch (Exception x)
            {
                return defaultvalue;
            }
        }

        public static bool ValidateReturnValue(string strjson, string root)
        {
            JObject obj = JObject.Parse(strjson);
            if (obj["sysErrors"].Count() > 0)
            {
                foreach (var err in obj["sysErrors"])
                    throw new MQException("403", err.ToString());

            }
            if (obj.ContainsKey(root))
            {
                if (obj[root]["errors"].Count() > 0)
                {
                    foreach (var err in obj[root]["errors"])
                        throw new MQException("403", err.ToString());
                }
            }
            else
            {
                throw new MQException("403",  $"返回的{strjson.Substring(0,50)}中不存在{root}");
            }
            return true;

        }

        /// <summary>
        /// 一次性把所有错误都显示出来
        /// </summary>
        /// <param name="strjson"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public static bool ValidateReturnValueV2(string strjson, string root)
        {
            JObject obj = JObject.Parse(strjson);
            List<string> lsterr = new List<string>();

            if (obj["sysErrors"].Count() > 0)
            {
                foreach (var err in obj["sysErrors"])
                    lsterr.Add(err.ToString());
                    //throw new MQException("403", err.ToString());

            }
            if (obj[root]["errors"].Count() > 0)
            {
                foreach (var err in obj[root]["errors"])
                    lsterr.Add(err.ToString());
                //throw new MQException("403", err.ToString());

            }

            if (lsterr.Count>0)
                throw new MQException("403",string.Join("#$%", lsterr.ToArray()));

            return true;

        }
        public static List<string> GetExceptionV2(string strjson, string key, List<string> defaultvalue, Dictionary<int, string> detailinfos=null)
        {
            try
            {
                List<string> lstSource = new List<string>(strjson.Split(new string[] { "#$%" },StringSplitOptions.RemoveEmptyEntries));


                List<string> Lstrv = new List<string>();
                foreach (var s in lstSource)
                {
                    JObject obj = JObject.Parse(s);
                    string message =string.IsNullOrWhiteSpace( obj[key].ToString())? obj["TITLE"].ToString(): obj[key].ToString();
                    message = message.Replace("\\u000a", Environment.NewLine);
                    if (detailinfos != null)
                    {
                        int errline = CommonHandler.GetErrorControlLine(s);
                        if (errline >= 0)
                            message += System.Environment.NewLine +  System.Environment.NewLine + detailinfos[errline];
                    }

                    Lstrv.Add(message);
                }
                return  Lstrv ;
            }
            catch (Exception x)
            {
                return defaultvalue;
            }
        }


        public static int GetErrorControlLine(string strjson)
        {
            try
            {
                JObject obj = JObject.Parse(strjson);
                string[] control = obj["ERRORCONTROL"].ToString().Split('.');
                return Convert.ToInt32( control[1]);
            }
            catch (Exception x)
            {
                return -1;
            }
        }



        
    }
}
